package com.aca.market.config;

import com.aca.market.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;

@Configuration
@Slf4j
public class MVConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //统一管理
//        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/blog.html").setViewName("blog");
        registry.addViewController("/cart.html").setViewName("cart");
        registry.addViewController("/category.html").setViewName("category");
        registry.addViewController("/checkout.html").setViewName("checkout");
        registry.addViewController("/confirmation.html").setViewName("confirmation");
        registry.addViewController("/contact.html").setViewName("contact");
        registry.addViewController("/register.html").setViewName("register");
        registry.addViewController("/single-blog.html").setViewName("single-blog");
        registry.addViewController("/single-product.html").setViewName("single-product");
        registry.addViewController("/tracking-order.html").setViewName("tracking-order");
        registry.addViewController("/shop.html").setViewName("shop");
        registry.addViewController("/shop/items/**").setViewName("shop");
        registry.addViewController("/shop/detail/**").setViewName("product-details");
        registry.addViewController("/pc").setViewName("product-single");
        registry.addViewController("/success.html").setViewName("success");
        registry.addViewController("/fail.html").setViewName("fail");
        registry.addViewController("/no-auth.html").setViewName("no-auth");
        for (String s : FileUtil.getUrl()) {
            log.info("enable mapping :" + s);
            registry.addViewController(s).setViewName(s);
        }
        log.info("======================addViewControllers");

    }

    // 自定模板路径
    @Bean
    public SpringResourceTemplateResolver fileTemplateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("classpath:/templates/bg/");
        log.info("===========新模板路径已添加");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML");
        resolver.setCacheable(false);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setOrder(1);
//        resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return resolver;
    }

    //    @Override
    //拦截器
//    public void addInterceptors(InterceptorRegistry registry) {
//        //加请求范围 拦截所有 排除静态资源
//        registry.addInterceptor(new LoginInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/","/login.html","/user/login","/css/**","/img/**","/js/**","/scss/**","/vendor/**","/carlist","/brand/all","/listcar/**");
////                .excludePathPatterns("/","/login.html","/user/login","/css/**","/img/**","/js/**","/scss/**","/vendor/**");
//    }

    //    @Bean
//    public ResourceBundleMessageSource messageSource(){
//        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
//        messageSource.setUseCodeAsDefaultMessage(true);
//        messageSource.setFallbackToSystemLocale(false);
//        messageSource.setBasenames("i18n/msg");
//        messageSource.setDefaultEncoding("UTF-8");
//        messageSource.setCacheSeconds(2);
//        return messageSource;
//    }

//    @Bean
//    public LocaleResolver localeResolver(){
//        return new LangResolver();
//    }

}
